package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author guozh
 */
@RestController
@RequestMapping("/admin/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 条件查询订单
     *
     * @param ordersPageQueryDTO 查询条件
     * @return Result<OrderVO>
     */
    @GetMapping("/conditionSearch")
    public Result<PageResult> conditionSearchOrder(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("条件查询订单:{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.conditionSearchOrder(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 各个状态的订单数量统计
     *
     * @return Result<OrderStatisticsVO>
     */
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> statistics() {
        log.info("订单统计");
        OrderStatisticsVO orderStatisticsVO = orderService.orderQuantityStatistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 查询订单详情
     *
     * @param id 订单id
     * @return Result<OrderVO>
     */
    @GetMapping("/details/{id}")
    public Result<OrderVO> findOrderDetail(@PathVariable Long id) {
        log.info("查询订单详情:{}", id);
        OrderVO orderVO = orderService.findOrderDetail(id);
        return Result.success(orderVO);
    }

    /**
     * 接单
     *
     * @param ordersConfirmDTO 接单信息
     * @return Result<String>
     */
    @PutMapping("/confirm")
    public Result<String> takeOrders(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("订单确认:{}", ordersConfirmDTO);
        orderService.takeOrders(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 拒绝订单
     * @param ordersConfirmDTO 拒绝订单信息
     * @return Result<String>
     */
    @PutMapping("/rejection")
    public Result<String> rejectionOrders(@RequestBody OrdersRejectionDTO ordersConfirmDTO) {
        log.info("订单拒绝:{}", ordersConfirmDTO);
        orderService.rejectionOrders(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 取消订单
     * @param ordersCancelDTO 取消订单信息
     * @return Result<String>
     */
    @PutMapping("/cancel")
    public Result<String> adminCancelOrders(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("订单取消:{}", ordersCancelDTO);
        orderService.adminCancelOrders(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 派送订单
     * @param id 订单id
     * @return Result<String>
     */
    @PutMapping("delivery/{id}")
    public Result<String> deliveryOrders(@PathVariable Long id) {
        log.info("订单派送:{}", id);
        orderService.deliveryOrders(id);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id 订单id
     * @return Result<String>
     */
    @PutMapping("complete/{id}")
    public Result<String> completeOrders(@PathVariable Long id) {
        log.info("订单完成:{}", id);
        orderService.completeOrders(id);
        return Result.success();
    }
}
